﻿/** v 0.0.1 2016-09-19 by majie **/
/**
*
*/
!function(paramWindow) {
   //window对象表示浏览器中打开的窗口
   paramWindow.STRUCTURE_H5INDEX_TEMPLATE={
	    /**构造数据div id="content" 的div元素**/
	    selfStructureIndexPageContent: function() {
            var contentDivDom = document.createElement("div");
            contentDivDom.id = "content";
            return contentDivDom;
        },
        /**构造header标签**/		
	    selfStructureIndexPageheader:function(){
			var headerDom = document.createElement("header");
			var paramDomItem=new Array();
			paramDomItem.push('<div class="header-wrap">');
			paramDomItem.push('<div id="search-placeholder"><span>我的工作</span></div>');
			paramDomItem.push('</div>');
			headerDom.innerHTML=paramDomItem.join("");
			return headerDom;
		},
		/**构造div scroll-view元素**/
		selfStructureIndexPagelayout: function() {
            var scrollViewDiv = document.createElement("div");
			scrollViewDiv.setAttribute("data-ctrl-name", "scroll-view");
			var paramDomItem=new Array();
			paramDomItem.push('<div class="scroll-wrap">');
			paramDomItem.push('<div class="scroll-content">');
			paramDomItem.push('</div>');
			paramDomItem.push('</div>');
            scrollViewDiv.innerHTML = paramDomItem.join("");
            return scrollViewDiv;
        }
		/**构造 元素**/
   }
} (window);
/**@构造需要的方法**/
!function(paramWindow, lib, ctrl){
	var point=0;
	var paramDocument = paramWindow.document;
	//var o = lib.promise.features;
    //var p = lib.promise.utilities;
	var createDivClassNameIsView=function (a, b) {
        var c = this;
        var d = paramDocument.createElement("div");
        d.className = "view";
        d.style.display = "none";
        d.setAttribute("id", "view-" + a.id);
        d.setAttribute("name", b.name);
        b = Object.create(b);
        Object.defineProperties(b, {
            viewport: {
                value: d,
                enumerable: !0,
                editable: !1
            },
            persisted: {
                get: function() {
                    return c.persisted
                },
                enumerable: !0,
                editable: !1
            },
            parameters: {
                value: Object.create(a.args),
                enumerable: !0,
                editable: !1
            }
        });
		/**@ this 代表 createDivClassNameIsView这个方法**/
        this.context = b;
        this.state = a;
        this.root = d;
    };
	var e=function (a) {
        function b(a) {
            return function() {
                var b, c, d = this,
                e = q;
                return d.aysnc = function() {
                    b = o.defer(),
                    e = b.promise
                },
                d.done = function() {
                    if (!b) throw new Error("call this.aysnc before use this.done");
                    b.resolve(c)
                },
                c = a.apply(d, arguments),
                c && c instanceof n ? c: e
            }
        }
        function c(a, b) {
            return n.all(a.map(function(a) {
                return a.call(b)
            }))
        }
        Object.defineProperty(this, "name", {
            value: a,
            enumerable: !0,
            editable: !1
        });
        var d = [],
        e = [];
        this.ready = function(a) {
            return "function" == typeof a ? (d.push(b(a)), this) : c(d, a)
        },
        this.meanwhile = function(a) {
            return "function" == typeof a ? (e.push(b(a)), this) : c(e, a)
        }
    };
	var f=function(a, b) {
        Object.defineProperty(this, "name", {
            value: a,
            enumerable: !0,
            editable: !1
        });
        this.startup = new e("startup"),
        this.show = new e("show"),
        this.hide = new e("hide"),
        this.teardown = new e("teardown"),
        b && b.call(this)
    }
	/**为body标签添加属性**/
	var bodyElementAddAttribute=function(paramBodyElement,elementParams){
		var i = [];
		var j = {};
		var paramDataCtrlId = Date.now() + "-" + ++point;
		paramBodyElement.setAttribute("data-ctrl-name", "pageview");
	    paramBodyElement.setAttribute("data-ctrl-id", paramDataCtrlId);
		var l = document.createDocumentFragment(); 
		! paramBodyElement || paramBodyElement instanceof HTMLElement || (elementParams = paramBodyElement, paramBodyElement = null);
        paramBodyElement || (paramBodyElement = document.createElement("div"), l.appendChild(paramBodyElement));
        elementParams = elementParams || {};
        paramBodyElement.setAttribute("data-ctrl-name", "pageview");
        paramBodyElement.setAttribute("data-ctrl-id", paramDataCtrlId);
        elementParams.fullscreen && (paramBodyElement.className = "fullscreen");
		var m = new createDivClassNameIsView({
            id: 0,
            args: {}
        },
        new f);
		this.push=function(a,b,d){
			var e = {
                name: a,
                args: b || {},
                action: "push",
                id: d || (new Date).getTime()
            };
			var f=this.getPage();
			var h=new createDivClassNameIsView(e,f);
			return h.context.startup.ready(h.context);
		}
		/**@**/
		this.getPage = function(a) {
            return j[a]
        }
		/**@**/
		this.definePage = function(a, b) {
            return j[a] = new f(a, b)
        }
	};
	/****/
	ctrl.pageview = function(paramBodyElement, elementParams) {
        return new bodyElementAddAttribute(paramBodyElement, elementParams)
    }
	
}(window, window.lib, window.ctrl || (window.ctrl = {}));
/**@**/
!function(a, b, c) {
    function d(a) {
        a = a || "";
        var b = this,
        c = {};
        if (a instanceof d && (a = a.toString()), a && "string" == typeof a) for (var e = a.split("&"), f = 0; f < e.length; f++) {
            var g = e[f].split("=");
            c[decodeURIComponent(g[0])] = decodeURIComponent(g[1])
        } else if ("object" == typeof a) for (var h in a) c[h] = a[h];
        for (var h in c) !
        function(a) {
            Object.defineProperty(b, a, {
                get: function() {
                    return c[a]
                },
                set: function(b) {
                    c[a] = b
                },
                enumerable: !0
            })
        } (h);
        this.toString = function() {
            return Object.keys(c).sort().map(function(a) {
                return encodeURIComponent(a) + "=" + encodeURIComponent(c[a])
            }).join("&")
        }
    }
    function e() {
        function b(a, b) {
            if (b) g.replace(a);
            else {
                var c = f.createElement("a");
                c.href = a,
                c.style.cssText = "display:none;",
                f.body.appendChild(c);
                var d;
                d = f.createEvent("HTMLEvents"),
                d.initEvent("click", !1, !1),
                d ? c.dispatchEvent(d) : g.href = a
            }
        }
        function c(a, b) {
            var c = f.createEvent("HTMLEvents");
            if (c.initEvent(a, !1, !1), b) for (var d in b) c[d] = b[d];
            window.dispatchEvent(c)
        }
        function e(a) {
            o.forEach(function(b) {
                b.call(this, a)
            })
        }
        function i() {
            this.exec = function() {
                e("navigation:start")
            }
        }
        function j() {
            this.exec = function() {
                e("navigation:push")
            }
        }
        function k() {
            this.exec = function() {
                e("navigation:pop")
            }
        }
        function l() {
            this.exec = function() {
                e("navigation:replace")
            }
        }
        function m(a, b) {
            return a.name === b.name && a.args.toString() === b.args.toString()
        }
        var n = [],
        o = [],
        p = !!h.state,
        q = {},
        r = "initialize";
        n.exec = function() {
            n.length && n.shift().exec()
        },
        this.delegate = function(a) {
            return o.indexOf(a) < 0 && o.push(a),
            this
        },
        this.push = function(a, e) {
            var f = {
                name: a,
                args: new d(e),
                id: q.state.id + 1
            };
            if (!m(f, q.state)) {
                r = "push";
                var g = f.args.toString();
                if (p) {
                    var i = "#" + f.name + (g ? "?" + g: "");
                    h.pushState({
                        name: f.name,
                        args: g,
                        id: f.id
                    },
                    null, i),
                    c("pushstate")
                } else {
                    var i = "#" + f.name + "[" + f.id + "]" + (g ? "?" + g: "");
                    b(i)
                }
                return this
            }
        },
        this.pop = function() {
            return q.state.id > 1 && (r = "pop", h.back()),
            this
        },
        this.replace = function(a, e) {
            var f = {
                name: a,
                args: new d(e),
                id: q.state.id
            };
            if (!m(f, q.state)) {
                r = "replace";
                var g = f.args.toString();
                if (p) {
                    var i = "#" + f.name + (g ? "?" + g: "");
                    h.replaceState({
                        name: f.name,
                        args: g,
                        id: f.id
                    },
                    null, i),
                    c("replacestate")
                } else {
                    var i = "#" + f.name + "[" + f.id + "]" + (g ? "?" + g: "");
                    b(i, !0)
                }
                return this
            }
        };
        var s = !1;
        this.start = function(b) {
            function c() {
                var a;
                if (p && null != h.state && h.state !== !0) a = {
                    id: h.state.id,
                    name: h.state.name,
                    args: new d(h.state.args)
                };
                else {
                    var c = g.hash,
                    e = b.defaultPath,
                    f = 1,
                    i = b.defaultArgs,
                    j = c.match(/#([^\[\]\?]+)(?:\[(\d+)\])?(?:\?(.*))?/);
                    j && (e = j[1], f = j[2], i = j[3]),
                    a = {
                        name: e || "",
                        id: parseInt(f || 1),
                        args: new d(i || "")
                    }
                }
                return a
            }
            function e(a) {
                var b = c(),
                d = q.state;
                q.state = b,
                b.id < d.id ? (r = "pop", n.push(new k)) : b.id === d.id ? "replace" === r ? n.push(new l) : console.error("请勿用location.hash/href/replace和history.back/go来改变hash值") : (r = "push", n.push(new j)),
                n.exec()
            }
            if (!s) {
                s = !0,
                p &= !!b.useHistoryState;
                var f = c();
                if (p) {
                    var m = f.args.toString(),
                    o = "#" + f.name + (m ? "?" + m: "");
                    g.hash !== o && h.pushState({
                        name: f.name,
                        args: m,
                        id: f.id
                    },
                    null, o),
                    setTimeout(function() {
                        a.addEventListener("pushstate", e, !1),
                        a.addEventListener("popstate", e, !1),
                        a.addEventListener("replacestate", e, !1)
                    },
                    1)
                } else {
                    var m = f.args.toString(),
                    o = "#" + f.name + "[" + f.id + "]" + (m ? "?" + m: "");
                    g.hash !== o && (g.hash = o),
                    setTimeout(function() {
                        a.addEventListener("hashchange", e, !1)
                    },
                    1)
                }
                return q.state = f,
                r = "start",
                n.push(new i),
                n.exec(),
                this
            }
        },
        Object.defineProperty(this, "useHistoryState", {
            get: function() {
                return p
            }
        }),
        Object.defineProperty(this, "action", {
            get: function() {
                return r
            }
        }),
        Object.defineProperty(this, "state", {
            get: function() {
                var a = q.state || {};
                return {
                    id: a.id,
                    name: a.name,
                    args: a.args
                }
            }
        })
    }
    var f = a.document,
    g = a.location,
    h = a.history,
    i = a.navigator.userAgent; !! i.match(/Firefox/i),
    !!i.match(/IEMobile/i); ! h.state && h.replaceState && h.replaceState(!0, null),
    b.navigation = function() {
        return new e
    }
} (window, window.lib || (window.lib = {})),
/**@执行构造需要的方法**/
!function(paramWindow,lib,ctrl,app){
	var paramDocument = paramWindow.document;
	var h = {};
	var paramMetaAppConfig = paramDocument.querySelector('meta[name="App-Config"]');
	if(paramMetaAppConfig){
		var paramMetaAppConfigContent=paramMetaAppConfig.getAttribute("content");
		var paramExpression=/([^=,]+)\=([^=,]+)[,\s]*/g;
		var paramAttributeKey;
		var paramAttributeValue;
		/**执行正则表达式**/
		for(var paramMetaAppConfigContentItem;paramMetaAppConfigContentItem=paramExpression.exec(paramMetaAppConfigContent);){
			/** "fullscreen=yes," 1:"fullscreen" ,2:"yes"**/
			paramAttributeKey = paramMetaAppConfigContentItem[1];
			paramAttributeValue = paramMetaAppConfigContentItem[2]; 
			/**如果m的值为yes那么返回的是true 如果m的值为no那么返回的是false**/
			("yes" === paramAttributeValue || "no" === paramAttributeValue) && (paramAttributeValue = !("yes" !== paramAttributeValue));
			h[paramAttributeKey] = paramAttributeValue;
		}
	}
    if (paramWindow.appConfig){
		for (var l in paramWindow.appConfig){
			h[l] = paramWindow.appConfig[l];
		}
	}
	/**{fullscreen: true, useHistoryState: true, transition: true}**/
	var p = new ctrl.pageview(paramDocument.body, {
        fullscreen: !!h.fullscreen,
        transition: !!h.transition
    });  
	app.config=h;
	app.pageview=p;
	app.definePage = function() {
        return p.definePage.apply(p, arguments);
    }
    app.getPage = function() {
        return p.getPage.apply(p, arguments);
    }
    app.pushPage = function() {
        return o.push.apply(o, arguments);
    }
    app.popPage = function() {
        return o.pop.apply(o, arguments);
    }
    app.replacePage = function() {
        return o.replace.apply(o, arguments);
    }
}(window, window.lib, window.ctrl, window.app || (window.app = {}));
/**@**/
!function(paramWindow,lib,ctrl){
	function d(a, b) {
        if ("string" == typeof b) a.innerHTML = b;
        else if (b instanceof HTMLElement) a.innerHTML = "",
        a.appendChild(b);
        else if (b instanceof Array || b instanceof NodeList) {
            var c = n.createDocumentFragment();
            Array.prototype.slice.call(b).forEach(function(a) {
                c.appendChild(a)
            }),
            a.appendChild(c)
        }
    }
	function f(e,f){
		 var j = this,
        o = Date.now() + "-" + ++u;
        1 !== arguments.length || arguments[0] instanceof HTMLElement || (f = arguments[0], e = null),
        f = f || {},
        e || (e = n.createElement("div"));
        var p = e.firstElementChild || n.createElement("div"),
        q = p.firstElementChild || n.createElement("div");
        p.parentNode || e.appendChild(p),
        q.parentNode || p.appendChild(q),
        e.setAttribute("data-ctrl-name", "scrollview"),
        e.setAttribute("data-ctrl-id", o),
        e.setAttribute("data-direction", "x" !== f.direction ? "vertical": "horizontal"),
        p.className.indexOf("scroll-wrap") < 0 && (p.className = p.className.split(" ").concat("scroll-wrap").join(" ").replace(/^\s+/, "")),
        q.className.indexOf("scroll-content") < 0 && (q.className = q.className.split(" ").concat("scroll-content").join(" ").replace(/^\s+/, "")),
        f.scrollElement = q,
        f.scrollWrap = p;
        //var r = new b.scroll(f);
        this.scrollWrap = p,
        this.scrollElement = q,
        //this.scroll = r,
        this.root = this.element = e;
		this.fixed=new h(this);
	}
	function d(a, b) {
        if ("string" == typeof b) a.innerHTML = b;
        else if (b instanceof HTMLElement) a.innerHTML = "",
        a.appendChild(b);
        else if (b instanceof Array || b instanceof NodeList) {
            var c = n.createDocumentFragment();
            Array.prototype.slice.call(b).forEach(function(a) {
                c.appendChild(a)
            }),
            a.appendChild(c)
        }
    }
	function h(a){
		var b, c, e, f, g = this,
        h = a.scroll,
        i = (n.createDocumentFragment(), !1);
		b = n.createElement("div");
		b.className = "top-fixed";
		b.style.cssText = "left: 0; top: 0; width: 100%;";
		Object.defineProperty(g, "topElement",  {
			get:function(){
				return b;
			},
			set:function(a){
				d(b,a);
			}
		});
		Object.defineProperty(g, "enable", {
            get: function() {
                return i
            },
            set: function(d) {
                i = d,
                i ? (b && (b.parentNode || a.root.insertBefore(b, a.scrollWrap), b.style.display = "block"), c && (c.parentNode || a.root.appendChild(c), c.style.display = "block"), e && (e.parentNode || a.root.insertBefore(e, a.scrollWrap), e.style.display = "block"), f && (f.parentNode || a.root.appendChild(f), f.style.display = "block")) : (topFiexElement && (b.style.display = "none"), c && (c.style.display = "none"), e && (e.style.display = "none"), f && (f.style.display = "none"))
            }
        });
		
	}
	var u=0;
	var n = paramWindow.document;
	ctrl.scrollView=function(a,b){
		return new f(a,b);
	}
}(window, window.lib, window.ctrl || (window.ctrl = {}));
/**@添加pageview需要的css样式**/
!function(){
	/**pageview所需要的css样式代码**/
	var loadElementCssItemArr=[];
	loadElementCssItemArr.push('[data-ctrl-name=pageview]{position:relative;width:100%;overflow-x:hidden}');
	loadElementCssItemArr.push('[data-ctrl-name=pageview]>.view{width:100%;display:none}');
	loadElementCssItemArr.push('[data-ctrl-name=pageview].fullscreen{height:100%;overflow:hidden}');
	loadElementCssItemArr.push('[data-ctrl-name=pageview].fullscreen>.view{height:100%;overflow:hidden}');
	loadElementCssItemArr.push('[data-ctrl-name=pageview] .view[animation="push:current"]{opacity:1;-webkit-animation:push-current .4s ease;-ms-animation:push-current .4s ease;-moz-animation:push-current .4s ease;animation:push-current .4s ease}');
	loadElementCssItemArr.push('@-webkit-keyframes push-current{0%{opacity:1}');
	loadElementCssItemArr.push('100%{opacity:0}}@keyframes push-current{0%{opacity:1}');
	loadElementCssItemArr.push('100%{opacity:0}}[data-ctrl-name=pageview] .view[animation="push:next"]{position:absolute;left:0;top:0;-webkit-transform:translateX(100%);-ms-transform:translateX(100%);-moz-transform:translateX(100%);transform:translateX(100%);-webkit-animation:push-next .4s ease;-ms-animation:push-next .4s ease;-moz-animation:push-next .4s ease;animation:push-next .4s ease}');
	loadElementCssItemArr.push('@-webkit-keyframes push-next{0%{-webkit-transform:translateX(100%);-ms-transform:translateX(100%);-moz-transform:translateX(100%);transform:translateX(100%)}');
	loadElementCssItemArr.push('100%{-webkit-transform:translateX(0);-ms-transform:translateX(0);-moz-transform:translateX(0);transform:translateX(0)}}@keyframes push-next{0%{-webkit-transform:translateX(100%);-ms-transform:translateX(100%);-moz-transform:translateX(100%);transform:translateX(100%)}');
	loadElementCssItemArr.push('100%{-webkit-transform:translateX(0);-ms-transform:translateX(0);-moz-transform:translateX(0);transform:translateX(0)}}[data-ctrl-name=pageview] .view[animation="pop:previous"]{opacity:0;-webkit-animation:pop-previous .4s ease;-ms-animation:pop-previous .4s ease;-moz-animation:pop-previous .4s ease;animation:pop-previous .4s ease}');
	loadElementCssItemArr.push('@-webkit-keyframes pop-previous{0%{opacity:0}');
	loadElementCssItemArr.push('100%{opacity:1}}@keyframes pop-previous{0%{opacity:0}');
	loadElementCssItemArr.push('100%{opacity:1}}[data-ctrl-name=pageview] .view[animation="pop:current"]{position:absolute;left:0;top:0;-webkit-transform:translateX(0);-ms-transform:translateX(0);-moz-transform:translateX(0);transform:translateX(0);-webkit-animation:pop-current .4s ease;-ms-animation:pop-current .4s ease;-moz-animation:pop-current .4s ease;animation:pop-current .4s ease}');
	loadElementCssItemArr.push('@-webkit-keyframes pop-current{0%{-webkit-transform:translateX(0);-ms-transform:translateX(0);-moz-transform:translateX(0);transform:translateX(0)}');
	loadElementCssItemArr.push('100%{-webkit-transform:translateX(100%);-ms-transform:translateX(100%);-moz-transform:translateX(100%);transform:translateX(100%)}}@keyframes pop-current{0%{-webkit-transform:translateX(0);-ms-transform:translateX(0);-moz-transform:translateX(0);transform:translateX(0)}');
	loadElementCssItemArr.push('100%{-webkit-transform:translateX(100%);-ms-transform:translateX(100%);-moz-transform:translateX(100%);transform:translateX(100%)}}');
    var cssStyle=loadElementCssItemArr.join("\n");
	var paramStyleElement = document.createElement("style");
	document.getElementsByTagName("head")[0].appendChild(paramStyleElement);
	/**@**/
	if (paramStyleElement.styleSheet){
		//注解: value = option||"init"
        //当option为true时, 即option有值, value就等于option.
        //当option为false时, 即option 为null 或undefined 时, value 为"init"
		paramStyleElement.styleSheet.disabled || (paramStyleElement.styleSheet.cssText = cssStyle);
	}else{
		try {
			paramStyleElement.innerHTML = cssStyle;
		} catch(exception) {
			paramStyleElement.innerText = cssStyle;
		}
	}
}();
/**@添加scrollview需要的css样式**/
!function(){
	/**scrollview所需要的css样式代码**/
	var loadElementCssItemArr=[];
	loadElementCssItemArr.push('[data-ctrl-name=scrollview]{width:100%;height:100%;overflow:hidden;display:-webkit-box;display:-moz-box;display:-ms-flexbox;display:-webkit-flex;display:flex}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview][data-direction=vertical]{-webkit-box-orient:vertical;-webkit-box-direction:normal;-moz-box-orient:vertical;-moz-box-direction:normal;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview][data-direction=horizontal]{-webkit-box-orient:horizontal;-webkit-box-direction:normal;-moz-box-orient:horizontal;-moz-box-direction:normal;-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview] .scroll-wrap{display:block;-webkit-box-flex:1;-moz-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;overflow:hidden;position:relative}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview][data-direction=vertical] .scroll-wrap,[data-ctrl-name=scrollview][data-direction=vertical] .scroll-content{width:100%}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview][data-direction=horizontal] .scroll-wrap,[data-ctrl-name=scrollview][data-direction=horizontal] .scroll-content{height:100%}');
	loadElementCssItemArr.push('[data-ctrl-name=scrollview] .scroll-content{position:absolute}');
	var cssStyle=loadElementCssItemArr.join("\n");
	var paramStyleElement = document.createElement("style");
	document.getElementsByTagName("head")[0].appendChild(paramStyleElement);
	/**@**/
	if (paramStyleElement.styleSheet){
		//注解: value = option||"init"
        //当option为true时, 即option有值, value就等于option.
        //当option为false时, 即option 为null 或undefined 时, value 为"init"
		paramStyleElement.styleSheet.disabled || (paramStyleElement.styleSheet.cssText = cssStyle);
	}else{
		try {
			paramStyleElement.innerHTML = cssStyle;
		} catch(exception) {
			paramStyleElement.innerText = cssStyle;
		}
	}
}();